package net.lahuobao.shunting.service.shunt;

import net.lahuobao.shunting.dao.TSDepartMapper;
import net.lahuobao.shunting.entity.shunt.DepartVO;
import net.lahuobao.shunting.entity.shunt.TSDepart;
import net.lahuobao.shunting.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Lenovo on 2017/2/7.
 */
@Service
public class DepartService extends BaseService{
    @Autowired
    public TSDepartMapper tsDepartMapper;

    public void saveDepart(Map map) throws Exception{
        TSDepart record = new TSDepart();
        record.setId(UUID.randomUUID().toString().replaceAll("-",""));
        record.setLng(Double.valueOf((String)map.get("lng")));
        record.setLat(Double.valueOf((String) map.get("lat")));
        record.setPoint("point("+record.getLat()+" "+ record.getLng() +")");
        record.setAddress((String)map.get("address"));
        if(map.containsKey("head") && "1".equals((String)map.get("head"))){
            record.setIsHead(true);
        }else{
            record.setIsHead(false);
        }
        record.setAreaCode((String)map.get("areaCode"));
        record.setDepartname((String) map.get("departname"));
        record.setDepartType(Integer.valueOf((String) map.get("departType")));
        record.setMobile((String) map.get("mobile"));
        record.setOrgCode((String) map.get("orgCode"));
        record.setOrgType((String)map.get("orgType"));
        record.setRemark((String)map.get("remark"));
        record.setParentdepartid((String)map.get("parentDepart"));

        int count = getDepartNumByTypeAndName(record.getDepartname(),record.getDepartType());
        if(count!=0){
            throw new Exception("组织名称已存在");
        }
        tsDepartMapper.insert(record);
    }

    public List<TSDepart> getDepartList(Map map){
        return tsDepartMapper.getDepartList(map);
    }
    public List<DepartVO> getDepartVOList(Map map){
        return tsDepartMapper.getDepartVOList(map);
    }
    public int getDepartCount(){
        return tsDepartMapper.getDepartCount();
    }
    public TSDepart selectByPrimaryKey(String id){
        return tsDepartMapper.selectByPrimaryKey(id);
    }
    public int updateDepart(TSDepart record,Map map){
        record.setRemark((String)map.get("remark"));
        record.setParentdepartid((String)map.get("parentDepart"));
        record.setAddress((String)map.get("address"));
        record.setDepartType(Integer.valueOf((String)map.get("departType")));
        record.setOrgCode((String)map.get("orgCode"));
        record.setMobile((String)map.get("mobile"));
        record.setAreaCode((String)map.get("areaCode"));
        record.setDepartname((String)map.get("departname"));
        if("1".equals((String)map.get("head"))) {
            record.setIsHead(true);
        }else{
            record.setIsHead(false);
        }
        record.setLng(Double.valueOf((String)map.get("lng")));
        record.setLat(Double.valueOf((String) map.get("lat")));
        record.setPoint("point("+record.getLat()+" "+ record.getLng() +")");
        return tsDepartMapper.updateByPrimaryKey(record);
    }

    public int deleteDepart(String id){
        return tsDepartMapper.deleteByPrimaryKey(id);
    }

    public int getDepartUserCount(String departId){
        return tsDepartMapper.getDepartUserCount(departId);
    }

    public int getDepartsCounts(String departId){
        return tsDepartMapper.getDepartsCounts(departId);
    }

    public List<TSDepart> selectByParentdepartId(String parentDepartId){
        return tsDepartMapper.selectByParentdepartId(parentDepartId);
    }

    /**
     *  更具类型获取门店信息
     * @param type 0 部门 1 门店 2 区域
     * @return
     */
    public List<DepartVO> getDepartVOListByType(int type){
        return tsDepartMapper.getDepartVOListByType(type);
    }
    /**
     * 获取指定部门类型和名称组织的数量
     * @param departName 组织名
     * @param departType 组织类型
     * @return
     */
    public int getDepartNumByTypeAndName(String departName,Integer departType){
        Map map = new HashMap();
        map.put("departName",departName);
        map.put("departType",departType);
        return tsDepartMapper.getDepartNumByTypeAndName(map);
    }
}
